var rootPath = getRootPath();

$(function() {
	$.ajax({
		url : rootPath + '/category/keyword',
		type : 'GET',
		success : function(data) {
			fillPage(data);
			$(".pageNavigate").hover(function() {
				$(this).css("cursor", "pointer");
			});
		},
		error : function(jqXHR, textStatus, errorThrown) {
			layer.alert("error:(");
		}
	});

	$.ajax({
		url : rootPath + '/category/pid/0', // 父级pid都为0
		type : 'GET',
		success : function(data) {
			fillselect(data);
		},
		error : function(jqXHR, textStatus, errorThrown) {
			layer.alert("error:(");
		}
	});

	$("#search").click(function() {
		var keyword = $("#keyword").val();
		var pid = $("#selectPid").val();
		$.ajax({
			url : rootPath + '/category/keyword',
			type : 'GET',
			data : {
				"keyword" : keyword,
				"pid" : pid
			},
			success : function(data) {
				fillPage(data);
			},
			error : function(jqXHR, textStatus, errorThrown) {
				layer.alert("error:(");
			}
		});
	});

})

function selectChange() {
	var keyword = $("#keyword").val();
	var pid = $("#selectPid").val();
	$.ajax({
		url : rootPath + '/category/keyword',
		type : 'GET',
		data : {
			"keyword" : keyword,
			"pid" : pid
		},
		success : function(data) {
			fillPage(data);
		},
		error : function(jqXHR, textStatus, errorThrown) {
			layer.alert("error:(");
		}
	});
}

function fillselect(data) {
	if (data.returnCode == "0000") {
		var length = data.returnObject.length;
		$("#selectPid").html(length);
		var option = "";
		option += "<option value = \"all\">全部分类</option>"
		for (var i = 0; i < length; i++) {
			option += "<option value =\"" + data.returnObject[i].categoryId
					+ "\">" + data.returnObject[i].title + "</option>";
		}
		$("#selectPid").append(option);
	} else {
		layer.alert(data.returnMessage);
	}
}

function fillPage(data) {
	if (data.returnCode == "0000") {
		var length = data.returnObject.list.length;
		$("#dataNum").html(data.returnObject.total);
		var tr;
		for (var i = 0; i < length; i++) {

			tr += "<tr class='text-c' id='category_"
					+ data.returnObject.list[i].categoryId
					+ "' name='categoryTr'>";
			tr += "<td><input type='checkbox' value='"
					+ data.returnObject.list[i].categoryId
					+ "' name='categoryCheckbox'></td>";
			var pidName = "";
			if (data.returnObject.list[i].hasOwnProperty("pidName")) {
				pidName = data.returnObject.list[i].pidName;
			}
			tr += "<td>" + data.returnObject.list[i].title + "</td>";
			tr += "<td>" + pidName + "</td>";
			tr += "<td class='f-14'>";
			tr += "<a title='编辑' href='javascript:;' onclick='category_add_edit(\"编辑分类\","
					+ "\"categoryEditPage?categoryId="
					+ data.returnObject.list[i].categoryId
					+ "\",\"\",\"\")' style='text-decoration:none'><i class='iconfont marginLeft12'>&#xe60a;</i></a>";
			tr += "<a title='删除' href='javascript:;' onclick='category_del(this,"
					+ "\""
					+ data.returnObject.list[i].categoryId
					+ "\")' class='ml-5' style='text-decoration:none'><i class='iconfont marginLeft12'>&#xe612;</i></a></td></tr>";

		}

		$("#categorys").empty();
		$("#categorys").append(tr);

		var page = "";
		page += "<span style='padding:7px 10px' class='pageNavigate' onclick='switchPage("
				+ 1 + ")'>首页</span>";
		if (data.returnObject.hasPreviousPage) {
			page += "<span style='padding:7px 10px' class='pageNavigate' onclick='switchPage("
					+ data.returnObject.prePage + ")'>上一页</span>";
		}
		var num = data.returnObject.navigatepageNums.length;
		for (var i = 0; i < num; i++) {
			if (data.returnObject.navigatepageNums[i] == data.returnObject.pageNum) {
				page += "<span style='font-weight:900;padding:7px 10px;'>"
						+ data.returnObject.pageNum + "</span>";
			} else {
				page += "<span style='padding:7px 10px;' class='pageNavigate' onclick='switchPage("
						+ data.returnObject.navigatepageNums[i]
						+ ")'>"
						+ data.returnObject.navigatepageNums[i] + "</span>";
			}
		}
		if (data.returnObject.hasNextPage) {
			page += "<span style='padding:7px 10px;' class='pageNavigate' onclick='switchPage("
					+ data.returnObject.nextPage + ")'>下一页</span>";
		}
		page += "<span style='padding:7px 10px' class='pageNavigate' onclick='switchPage("
				+ data.returnObject.pages + ")'>末页</span>";

		$("#page").empty();
		$("#page").append(page);
	}
}

function switchPage(pageNo) {
	$(this).css("cursor", "pointer");
	var keyword = $("#keyword").val();
	var pid = $("#selectPid").val();
	$.ajax({
		url : rootPath + '/category/keyword',
		type : 'GET',
		data : {
			"keyword" : keyword,
			"pageNo" : pageNo,
			"pid" : pid
		},
		success : function(data) {
			fillPage(data);
			$(".pageNavigate").hover(function() {
				$(this).css("cursor", "pointer");
			});
		},
		error : function(jqXHR, textStatus, errorThrown) {
			layer.alert("error:(");
		}
	});
}

function category_del(obj, id) {
	var dataNum = parseInt($("#dataNum").html());
	layer.confirm('删除操作须谨慎，若删除的分类有子分类，则同时删除子分类，确认要删除吗？', function(index) {
		if (id != "") {
			$.ajax({
				url : rootPath + '/category/' + id,
				type : 'DELETE',
				dataType : 'json',
				success : function(data) {
					if (data.returnCode == "0000") {
						// $(obj).parents("tr").remove();
						layer.msg('已删除!', {
							icon : 1,
							time : 1000
						});
						// $("#dataNum").html(dataNum - 1);
						$.ajax({
							url : rootPath + '/category/keyword',
							type : 'GET',
							data : {},
							success : function(data) {
								fillPage(data);
							},
							error : function(jqXHR, textStatus, errorThrown) {
								layer.alert("error:(");
							}
						});
						
						
						$.ajax({
							url : rootPath + '/category/pid/0', 
							type : 'GET',
							success : function(data) {
								fillselect(data);
							},
							error : function(jqXHR, textStatus, errorThrown) {
								layer.alert("error:(");
							}
						});

					} else {
						layer.alert(data.returnMessage);
					}
				},
				error : function(data) {
					layer.aler("error:(");
				},
			});
		}
	});
}

function datadel() {

	var categoryIds = new Array();
	$("input:checkbox[name='categoryCheckbox']:checked").each(function(k) {
		categoryIds.push($(this).val());
	});
	var length = categoryIds.length;
	var dataNum = parseInt($("#dataNum").html());
	if (length > 0) {
		layer.confirm('删除操作须谨慎，若删除的分类有子分类，则同时删除子分类，确认要删除吗？', function(index) {
			$.ajax({
				url : rootPath + '/category/categoryIds',
				type : 'DELETE',
				traditional : true,
				data : JSON.stringify(categoryIds),
				contentType : 'application/json;charset=UTF-8',
				dataType : "json",
				success : function(data) {
					if (data.returnCode == "0000") {
						layer.msg('已删除!', {icon : 1,time : 1000});
						$.ajax({
							url : rootPath + '/category/keyword',
							type : 'GET',
							data : {},
							success : function(data) {
								fillPage(data);
							},
							error : function(jqXHR, textStatus, errorThrown) {
								layer.alert("error:(");
							}
						});
						
						$.ajax({
							url : rootPath + '/category/pid/0', 
							type : 'GET',
							success : function(data) {
								fillselect(data);
							},
							error : function(jqXHR, textStatus, errorThrown) {
								layer.alert("error:(");
							}
						});
						
					} else {
						layer.alert(data.returnMessage);
					}
				},
				error : function(data) {
					layer.alert("error:(");
				},
			});
		});
	} else {
		layer.alert("请选择需要删除的分类!!!");
	}
}

/* 管理员-分类-添加-编辑 */
function category_add_edit(title, url, w, h) {
	layer_show(title, url, w, h);
}